/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: cbus_service.h
 *
 * Purpose: the service side API define for cbus
 *
 * Developer:
 *   wen.gu , 2021-06-15
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __CBUS_SERVICE_H__
#define __CBUS_SERVICE_H__

#include "cbus/core/cbus_type.h"
#include "cbus/core/cbus_error.h"
#include "cbus/bus/cbus_message.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
G_C_HEADER_START

struct _cbus_service;
typedef struct _cbus_service* cbus_service_t;

typedef void (*cbus_service_method)(cbus_message_t request_message);

typedef struct _cbus_service_method_item
{
    cbus_message_id method_id;
    GPTR opaque;
    cbus_service_method on_method_call;
}cbus_service_method_item_t;

typedef void (*cbus_service_property_method)(cbus_message_t request_message);


/** compare the value of property whether changed(or updated) , GTRUE: changed, GFALSE: not change */
typedef GBOL (*cbus_service_property_compare_value_func)(const cbus_message_t old_value, const cbus_message_t new_value);

typedef struct _cbus_service_property_item
{
    cbus_message_id property_id;
    GBOL has_notification;
    GPTR opaque;
    cbus_service_property_method on_set;
    cbus_service_property_method on_get;   
    cbus_service_property_compare_value_func on_compare; /** is NULL, will use default compare: memcmp */
    cbus_message_t default_value;  /** default value,  the property must be have a default value */ 
}cbus_service_property_item_t;


/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

/** use 'NULL' as the end of url_array */
G_API GErrc cbus_service_create(cbus_service_t* srv, const GSTR* url_array);
G_API void cbus_service_destroy(cbus_service_t srv);

G_API GErrc cbus_service_start(cbus_service_t srv);
G_API GErrc cbus_service_stop(cbus_service_t srv);

//G_API cbus_message_t cbus_service_alloc_message(cbus_service_t srv, cbus_message_type_t message_type, cbus_message_id message_id, GU32 payload_size);

G_API GErrc cbus_service_method_register(cbus_service_t srv, cbus_message_id method_id, cbus_service_method method, GPTR opaque);
G_API GErrc cbus_service_method_register_group(cbus_service_t srv, const cbus_service_method_item_t* group, GU32 group_size);

G_API GErrc cbus_service_method_unregister(cbus_service_t srv, cbus_message_id method_id);
G_API GErrc cbus_service_method_unregister_all(cbus_service_t srv);

G_API GErrc cbus_service_method_send_response(cbus_service_t srv, cbus_message_t message);

G_API GErrc cbus_service_method_send_response_error(cbus_service_t srv, cbus_message_t request_message, GU08 error_code);

G_API GErrc cbus_service_event_notify(cbus_service_t srv, cbus_message_t event_data);

G_API GErrc cbus_service_property_register(cbus_service_t srv, const cbus_service_property_item_t* property_info);
G_API GErrc cbus_service_property_register_group(cbus_service_t srv, const cbus_service_property_item_t* group, GU32 group_size);
G_API GErrc cbus_service_property_unregister(cbus_service_t srv, cbus_message_id property_id);
G_API GErrc cbus_service_property_unregister_all(cbus_service_t srv);


G_API GErrc cbus_service_property_update(cbus_service_t srv, cbus_message_t property_value);



G_C_HEADER_END
#endif /** !__CBUS_SERVICE_H__ */

